

Ext.override(Ext.grid.GridPanel, {
	getExcelXml : function(includeHidden, title) {
		var worksheet = this.createWorksheet(includeHidden, title);
		var totalWidth = this.getColumnModel().getTotalWidth(includeHidden);
		return '<?xml version="1.0" encoding="utf-8"?>\n'
				+ '<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office">\n'
				+ '<o:DocumentProperties><o:Title>'
				+ this.title
				+ '</o:Title></o:DocumentProperties>\n'
				+ '<ss:ExcelWorkbook>\n'
				+ '<ss:WindowHeight>'
				+ worksheet.height
				+ '</ss:WindowHeight>\n'
				+ '<ss:WindowWidth>'
				+ worksheet.width
				+ '</ss:WindowWidth>\n'
				+ '<ss:ProtectStructure>False</ss:ProtectStructure>\n'
				+ '<ss:ProtectWindows>False</ss:ProtectWindows>\n'
				+ '</ss:ExcelWorkbook>\n'
				+ '<ss:Styles>\n'
				+ '<ss:Style ss:ID="Default" ss:Name="Normal">\n'
				+ '<ss:Alignment ss:Vertical="Center"  ss:WrapText="1" />\n'
				+ '<ss:Font ss:FontName="宋体" ss:Size="10" ss:Color="#000000" />\n'
				+ '<ss:Interior />\n'
				+ '<ss:NumberFormat />\n'
				+ '<ss:Protection />\n'
				+ '</ss:Style>\n'
				+ '<ss:Style ss:ID="title">\n'
				+ '<ss:Borders />\n'
				+ '<ss:Font ss:Size="16" ss:FontName="宋体" x:CharSet="134" ss:Bold="1"/>\n'
				+ '<ss:Alignment ss:WrapText="1" ss:Vertical="Center" ss:Horizontal="Center" />\n'
				+ '<ss:NumberFormat ss:Format="@" />\n'
				+ '</ss:Style>\n'
				+ '<ss:Style ss:ID="headercell">\n'
				+ '<ss:Font ss:Size="11" ss:FontName="宋体" x:CharSet="134" ss:Bold="1"/>\n'
				+ '<ss:Alignment ss:WrapText="1" ss:Vertical="Center" ss:Horizontal="Center" />\n'
				+ '<ss:Interior   ss:Color="#33CCCC" ss:Pattern="Solid" />\n'
				+ '<ss:Borders>\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Top" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Bottom" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Left" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Right" />\n'
				+ '</ss:Borders>\n'
				+ '</ss:Style>\n'
				+ '<ss:Style ss:ID="even">\n'
				+ '<ss:Font ss:FontName="宋体" ss:Size="10" ss:Color="#000000" />\n'
				+ '<ss:Interior ss:Pattern="Solid"  />\n'
				+ '<ss:Borders>\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Top" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Bottom" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Left" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Right" />\n'
				+ '</ss:Borders>\n'
				+ '</ss:Style>\n'
				+ '<ss:Style ss:Parent="even" ss:ID="evendate">\n'
				+ '<ss:NumberFormat ss:Format="[ENG][$-409]dd\-mmm\-yyyy;@" />\n'
				+ '</ss:Style>\n'
				+ '<ss:Style ss:Parent="even" ss:ID="evenint">\n'
				+ '<ss:NumberFormat ss:Format="0" />\n'
				+ '<ss:Borders>\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Top" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Bottom" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Left" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Right" />\n'
				+ '</ss:Borders>\n'
				+ '</ss:Style>\n'
				+ '<ss:Style ss:Parent="even" ss:ID="evenfloat">\n'
				+ '<ss:NumberFormat ss:Format="0.00" />\n'
				+ '<ss:Borders>\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Top" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Bottom" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Left" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Right" />\n'
				+ '</ss:Borders>\n'
				+ '</ss:Style>\n'
				+ '<ss:Style ss:Parent="even" ss:ID="evenpercent">\n'
				+ '<ss:NumberFormat ss:Format="Percent" />\n'
				+ '<ss:Borders>\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Top" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Bottom" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Left" />\n'
				+ '<ss:Border  ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Right" />\n'
				+ '</ss:Borders>\n' + '</ss:Style>\n' + '</ss:Styles>\n'
				+ worksheet.xml + '</ss:Workbook>\n';
	},

	createWorksheet : function(includeHidden, title) {

		// Calculate cell data types and extra class names which affect
		// formatting
		var cellType = [];
		var cellTypeClass = [];
		var cm = this.getColumnModel();
		var totalWidthInPixels = 0;
		var colXml = '';
		var headerXml = '';
		var visibleColumnCountReduction = 0;
		var colCount = cm.getColumnCount();
		for (var i = 0; i < colCount; i++) {
			if (cm.getDataIndex(i) && (includeHidden || !cm.isHidden(i))) {
				var w = cm.getColumnWidth(i);
				var titleHeader = cm.getColumnHeader(i);
				if (Ext.isEmpty(titleHeader)) {
					titleHeader = "";
				}
				titleHeader = titleHeader.replace("<br>", "");
				totalWidthInPixels += w;
				colXml += '<ss:Column ss:AutoFitWidth="1" ss:Width="' + w
						+ '" />\n';
				headerXml += '<ss:Cell ss:StyleID="headercell">\n'
						+ '<ss:Data ss:Type="String">' + titleHeader
						+ '</ss:Data>\n'
						+ '<ss:NamedCell ss:Name="Print_Titles" /></ss:Cell>\n';
				var fld = this.store.recordType.prototype.fields.get(cm
						.getDataIndex(i));
				var type = "string";
				if (fld) {
					try {
						type = fld.type.type;
					} catch (err) {
						type = "string";
					}
				}
				switch (type) {
					case "int" :
						cellType.push("Number");
						cellTypeClass.push("int");
						break;
					case "float" :
						cellType.push("Number");
						cellTypeClass.push("float");
						break;
					case "bool" :
					case "boolean" :
						cellType.push("String");
						cellTypeClass.push("");
						break;
					case "date" :
						cellType.push("String");
						cellTypeClass.push("");
						break;
					default :
						cellType.push("String");
						cellTypeClass.push("");
						break;
				}
			}
		}
		var visibleColumnCount = cellType.length;

		var result = {
			height : 9000,
			width : Math.floor(totalWidthInPixels * 30) + 50
		};
		var sheetTitle = title || this.title || " ";
		// Generate worksheet header details.
		var t = '<ss:Worksheet ss:Name="'
				+ sheetTitle
				+ '">\n'
				+ '<ss:Names>\n'
				+ '<ss:NamedRange ss:Name="Print_Titles" ss:RefersTo="=\''
				+ sheetTitle
				+ '\'!R1:R2" />\n'
				+ '</ss:Names>\n'
				+ '<ss:Table x:FullRows="1" x:FullColumns="1"\n'
				+ ' ss:ExpandedColumnCount="'
				+ visibleColumnCount
				+ '" ss:ExpandedRowCount="'
				+ (this.store.getCount() + 2)
				+ '">\n'
				+ colXml
				+ '<ss:Row ss:Height="38">\n'
				+ '<ss:Cell ss:StyleID="title" ss:MergeAcross="'
				+ (visibleColumnCount - 1)
				+ '">\n'
				+ '<ss:Data xmlns:html="http://www.w3.org/TR/REC-html40" ss:Type="String">\n'
				+ '<html:B><html:Font html:Size="15">\n' + sheetTitle
				+ '</html:Font></html:B>\n'
				+ '</ss:Data><ss:NamedCell ss:Name="Print_Titles" />\n'
				+ '</ss:Cell>\n' + '</ss:Row>\n'
				+ '<ss:Row ss:AutoFitHeight="1">\n' + headerXml + '</ss:Row>\n';
		// Generate the data rows from the data in the Store
		for (var i = 0, it = this.store.data.items, l = it.length; i < l; i++) {
			t += '<ss:Row>\n';
			var cellClass = 'even';
			var rd = it[i];
			r = rd.data;
			var k = 0;
			// 循环列
			for (var j = 0; j < colCount; j++) {
				var cellDateType = cellType[k];
				var styleID = cellClass + cellTypeClass[k];
				var filed = cm.getDataIndex(j);
				if (filed && (includeHidden || !cm.isHidden(j))) {
					var editor = cm.config[j].getEditor(0);
					var v = r[filed];
					if (Ext.isEmpty(v)) {
						v = "";
					}
				   if (editor && editor.isXType('combo', false)) {
						var record = editor.findRecord(editor.valueField, v);
						v = record ? record.get(editor.displayField) : "";
					} else {
						var renderer = cm.config[j].renderer;
						if (Jinyi.project.Format.percentRenderer === renderer) {
							cellDateType = "Number";
							styleID = "evenpercent";
						} else {
							v = renderer(v, {}, rd, i, j, this.store);
							if (!Ext.isNumber(v)) {
								cellDateType = "String";
								styleID = "even";
							}
						}
					}
					t += '<ss:Cell ss:StyleID="' +styleID +'">\n<ss:Data ss:Type="'
							+ cellDateType + '">';
					t += v;
					t += '</ss:Data>\n</ss:Cell>\n';
					k++;
				}
			}
			t += '</ss:Row>\n';
		}

		result.xml = t
				+ '</ss:Table>\n'
				+ '<x:WorksheetOptions>\n'
				+ '<x:PageSetup>\n'
				+ '<x:Layout x:CenterHorizontal="1" x:Orientation="Landscape" />\n'
				+ '<x:Footer x:Data="Page &amp;P of &amp;N" x:Margin="0.5" />'
				+ '<x:PageMargins x:Top="0.5" x:Right="0.5" x:Left="0.5" x:Bottom="0.8" />\n'
				+ '</x:PageSetup>\n' + '<x:FitToPage />\n' + '<x:Print>\n'
				+ '<x:PrintErrors>Blank</x:PrintErrors>\n'
				+ '<x:FitWidth>1</x:FitWidth>\n'
				+ '<x:FitHeight>2</x:FitHeight>\n' + '<x:ValidPrinterInfo />\n'
				+ '<x:VerticalResolution>600</x:VerticalResolution>\n'
				+ '</x:Print>\n' + '<x:Selected />\n'
				+ '<Panes> <Pane><Number>3</Number>\n'
				+ '<ActiveRow>1</ActiveRow>\n '
				+ '<RangeSelection>R2</RangeSelection>\n '
				+ '</Pane> </Panes>\n'
				+ '<x:ProtectObjects>False</x:ProtectObjects>\n'
				+ '<x:ProtectScenarios>False</x:ProtectScenarios>\n'
				+ '</x:WorksheetOptions>\n' + '</ss:Worksheet>';
		return result;
	}
});

/*******************************************************************************
 * 
 * 导出excel 控制
 */
function exportExcelF(grid, title) {
	var vExportContent = grid.getExcelXml(false, title);
	var fd = Ext.get('frmDummy');
	if (!fd) {
		fd = Ext.DomHelper.append(Ext.getBody(), {
					tag : 'form',
					method : 'post',
					id : 'frmDummy',
					target : '_blank',
					action : '/' + appConfig.appName + '/exportexcel.jsp',
					name : 'frmDummy',
					cls : 'x-hidden',
					cn : [{
								tag : 'input',
								name : 'exportContent',
								id : 'exportContent',
								type : 'hidden'
							}, {
								tag : 'input',
								name : 'fileName',
								id : 'fileName',
								type : 'hidden'
							}]
				}, true);
	}
	fd.child('#exportContent').set({
				value : vExportContent
			});
	fd.child('#fileName').set({
				value : title
			});
	fd.dom.submit();
}